var salt = sessionStorage.getItem('usersalt');

layui.use(['form', 'layer', 'laydate', 'table'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		formSelects = layui.formSelects,
		table = layui.table;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}

	/*时间选择器*/
	laydate.render({
		elem: '#statdate',
		trigger: 'click'
	});

	laydate.render({
		elem: '#enddate',
		trigger: 'click'
	});

	/*筛选*/
	$('#seleCheck').on('click', function() {
		getSecurityCheckList();
	});

	/*搜索*/
	$('#findscri').on('click', function() {
		var inptext = $('#inptext').val();
		var box = /\s/g;
		var param = inptext.replace(box, '');
		getSecurityCheckList(param);
	});
	var company;
	/*取出所有的单位*/
	function getAllDanwei() {
		$.ajax({
			url: url + 'baseEnt/getAllEnt',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {},
			success: function(data) {
				$('#danwei').empty();
				if(data.code == 0) {
					company = data.data;
				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	}
	getAllDanwei();

	/*------------tree.start------------*/
	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck: zTreeOnCheck
		}
	};
	var zNodes = company;
	$.fn.zTree.init($("#danwei"), setting, zNodes);

	function getSydwVal() {
		var treeArr = [];
		var treeObj = $.fn.zTree.getZTreeObj("danwei");
		var nodes = treeObj.getCheckedNodes(true);
		for(var i = 0; i < nodes.length; i++) {
			treeArr.push(nodes[i].id); //获取选中节点的值
		}
		return treeArr;
	}
	$('#resclas').click(function() {
		$("body").trigger("click");
		$('#danwei').show();

	});

	function zTreeOnCheck(event, treeId, treeNode) {
		var nval = "";
		var treeObj = $.fn.zTree.getZTreeObj("danwei");
		var nodes = treeObj.getCheckedNodes(true);
		for(var i = 0; i < nodes.length; i++) {
			nval += nodes[i].name + ",";
		}
		$('#resclas').val(nval.substr(0, nval.length - 1));
	};
	/*------------tree.end------------*/

	var tableIns;
	//安全检查列表
	function getSecurityCheckList(searchText) {
		var sydwArr = getSydwVal();
		if(!searchText) {
			searchText = null;
		}

		tableIns = table.render({
			elem: '#checkList',
			method: 'post',
			url: url + 'checkInfo/getPage',
			where: {
				Token: salt, //用户码
				searchText: searchText, //搜索文本框内容
				sydwIds: sydwArr.toString(), //适用单位ids
				type: $("#type").val(), //检查分类
				startTime: $('#statdate').val(), //开始时间
				endTime: $('#enddate').val(), //结束时间

			},
			cellMinWidth: 95,
			page: true,
			height: "full-115",
			id: "checkListTable",
			cols: [
				[{
						type: "numbers",
						width: 50,
						title: "序号"
					},
					{
						type: "checkbox",
						width: 50
					},
					{
						field: 'name',
						title: '检查表名称',
						align: 'center',
						templet: function(p) {
							if(p.name == null) {
								return "<div></div>";
							} else {
								return "<div onclick=showDetail('" + p.id + "')>" + p.name + "</div>";
							}
						}
					},
					{
						field: 'applyEntName',
						title: '适用单位',
						align: 'center',
						templet: function(p) {
							if(p.applyEntName == null) {
								return "<div></div>";
							} else {
								return "<div onclick=showDetail('" + p.id + "')>" + p.applyEntName + "</div>";
							}
						}
					},
					{
						field: 'type',
						title: '检查分类',
						align: 'center',
						templet: function(p) {
							if(p.type == null) {
								return "<div></div>";
							}
							var typeName = "";
							if(p.type == 1) {
								typeName = "日常检查";
							}
							if(p.type == 2) {
								typeName = "专项检查";
							}
							if(p.type == 3) {
								typeName = "季度检查";
							}
							return "<div onclick=showDetail('" + p.id + "')>" + typeName + "</div>";
						}
					},
					{
						field: 'crtTime',
						title: '创建日期',
						align: 'center',
						templet: function(p) {
							if(p.crtTime == null) {
								return "<div></div>";
							} else {
								return "<div onclick=showDetail('" + p.id + "')>" + p.crtTime + "</div>";
							}
						}
					}
				]
			],
			done: function(res, curr, count) {
				
			}
		});

	}
	getSecurityCheckList();

	//编辑
	$(".edit_btn").click(function() {
		var checkStatus = table.checkStatus('checkListTable'),
			data = checkStatus.data,
			editId = "";

		if(data.length > 1) {
			layer.msg("只能选择一条数据");
			return;
		}
		if(data[0].userId != salt) {
			layer.msg("只能编辑本人发起的安全检查");
			return;
		}

		if(data.length > 0) {
			editId = data[0].id;
			location.href = '../check-add/check-add.html?id=' + editId;
		} else {
			layer.msg("请选择需要编辑的数据");
		}
	});

	//批量删除
	$(".delAll_btn").click(function() {
		var checkStatus = table.checkStatus('checkListTable'),
			data = checkStatus.data;

		var deleteIdArr = new Array();
		for(var i = 0; i < data.length; i++) {
			deleteIdArr.push(data[i].id);
		}
		if(data.length > 0) {
			layer.confirm('确定删除选中的数据吗？', {
				icon: 3,
				title: '提示信息'
			}, function(index) {
				$.ajax({
					url: url + 'checkInfo/delCheckInfo',
					dataType: 'json',
					async: false,
					type: "POST",
					data: {
						Token: salt,
						ids: deleteIdArr.toString()
					},
					success: function(data) {
						if(data.code == 0) {
							layer.msg('删除成功');
						} else {
							layer.msg(data.msg);
						}

					},
					error: function(error) {
						console.log("获取失败");
					}
				});

				tableIns.reload();
				layer.close(index);
			})
		} else {
			layer.msg("请选择需要删除的数据");
		}
	});

	//导出
	$(".export_btn").click(function() {
		var checkStatus = table.checkStatus('checkListTable'),
		data = checkStatus.data;
		if(data.length > 0) {
			if(data.length > 1) {
				layer.msg("一次只能导出一条数据");
				return;
			}
			$.post(url + 'checkInfo/exportInfo', {
				Token: salt,
				id: data[0].id
			}, function(result) {
				window.open(url + "file" + result.data);
			});
		} else {
			layer.msg("请选择需要导出的数据");
		}
	});

	//导入
	$(".import_btn").click(function() {
		layer.open({
			type: 2,
			title: false,
			content: "page/import.html?url=checkInfo",
			area: ['700px', '300px'],
			cancel: function(index, layero){ 
				tableIns.reload();
		}
		});
	});
})

function showDetail(checkId) {
	location.href = '../check-detail/check-detail.html?id=' + checkId;
}